﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;

namespace VerticalShooterRPG
{
    class playerShip : BasicModel
    {
        Matrix _rotation = Matrix.Identity;
        Matrix _position = Matrix.Identity;
        bool _inMidBarrelRoll;
        float _shipSpeed;

        public playerShip(Model m) : base(m)
        {
            _shipSpeed = 0.6f;
            // initial position of ship
            _rotation = Matrix.CreateRotationY(MathHelper.PiOver2) * Matrix.CreateRotationX(MathHelper.PiOver2);
            _inMidBarrelRoll = false; // TODO: Barrel Roll
        }

        public override void Update()
        {
            // player actions 
            UpdateControls(Keyboard.GetState(), Mouse.GetState());


        }

        private void UpdateControls(KeyboardState keyState, MouseState mouseState)
        {
            UpdateMovementControls(keyState);
            UpdateAttackControls(mouseState);
        }

        private void UpdateAttackControls(MouseState mouseState)
        {
            if (mouseState.LeftButton == ButtonState.Pressed)
            {
                Point mousePos = new Point(mouseState.X, mouseState.Y);
                Point shipPos = new Point();

                
            }
        }

        private void UpdateMovementControls(KeyboardState keyState)
        {
            if (keyState.IsKeyDown(Keys.A) && _position.Translation.X > -100)
            {
                _position *= Matrix.CreateTranslation(-_shipSpeed, 0, 0);
                // Tilt the ship to the left
                if (_rotation.M21 > -0.6f)
                    _rotation *= Matrix.CreateFromAxisAngle(-Vector3.UnitY, MathHelper.PiOver4 / 10);
            }
            if (keyState.IsKeyDown(Keys.D) && _position.Translation.X < 100)
            {
                _position *= Matrix.CreateTranslation(_shipSpeed, 0, 0);

                // Tilt the ship to the right
                if (_rotation.M21 < 0.8f)
                    _rotation *= Matrix.CreateFromAxisAngle(Vector3.UnitY, MathHelper.PiOver4 / 10);
            }
            if (keyState.IsKeyDown(Keys.S) && _position.Translation.Y > -60)
            {
                _position *= Matrix.CreateTranslation(0, -_shipSpeed, 0);
            }
            if (keyState.IsKeyDown(Keys.W) && _position.Translation.Y < 56)
            {
                _position *= Matrix.CreateTranslation(0, _shipSpeed, 0);
            }

            // Reset the player's ship
            bool inRange = _rotation.M21 < 0.01 & _rotation.M21 > -0.01; // range due to using floating point number (not good to check against 0)
            if (!keyState.IsKeyDown(Keys.D) && !keyState.IsKeyDown(Keys.A) && !inRange)
            {
                // reset the planes left/right tilt
                if (_rotation.M21 < 0)
                    _rotation *= Matrix.CreateFromAxisAngle(Vector3.UnitY, MathHelper.PiOver4 / 10);
                if (_rotation.M21 > 0)
                    _rotation *= Matrix.CreateFromAxisAngle(-Vector3.UnitY, MathHelper.PiOver4 / 10);
            }
        }

        public override Matrix GetWorld()
        {
            return world * _rotation * _position;
        }
    }
}
